package cn.car91.activity;

import android.os.Bundle;
import android.widget.TextView;
import cn.car91.R;
import cn.car91.bean.User;
import cn.car91.http.HttpHandler;
import cn.car91.utils.Car91JsonUtil;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;

public class UserInfoActivity extends BaseActivity {

    @ViewInject(R.id.user_info_nick)
    TextView nickText;
    @ViewInject(R.id.user_info_name)
    TextView nameText;
    @ViewInject(R.id.user_info_sex)
    TextView sexText;
    @ViewInject(R.id.user_info_birth)
    TextView birthText;
    @ViewInject(R.id.user_info_address)
    TextView addressText;
    @ViewInject(R.id.user_info_zip)
    TextView zipText;
    @ViewInject(R.id.user_info_mobile)
    TextView mobileText;
    @ViewInject(R.id.user_info_email)
    TextView emailText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        setContentView(R.layout.activity_user_info);
        super.onCreate(savedInstanceState);

        ((TextView) findViewById(R.id.header_text)).setText("个人资料");

        if (getCar91Application().getUser() != null) {
        	HttpHandler.httpHandler.getUserDetail(getCar91Application().getUser().getId(), callBackUser);
        }
    }

    /*@OnClick(R.id.user_info_edit)
    public void onEditClick(View view) {
    	TextView v = (TextView) view;
    	if(v.getText().toString().equals(getResources().getString(R.string.user_edit_btn))){
    		
    	}else if(v.getText().toString().equals(getResources().getString(R.string.user_edit_submit_btn))){
    		
    	}
    }*/
    
    /*@OnClick(R.id.user_info_qrcode)
    public void onQRCodeClick(View view) {
    	TextView v = (TextView) view;
    	if(v.getText().toString().equals(getResources().getString(R.string.user_qrcode_btn))){
    		String format = "png";// 图像类型
    		
    		//Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();  
            //hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");  
            //BitMatrix bitMatrix = new MultiFormatWriter().encode(UserInfoActivity.this,  
                    //BarcodeFormat.QR_CODE, width, height, hints);// 生成矩阵
            //Path path = FileSystems.getDefault().getPath(filePath, fileName);  
            //MatrixToImageWriter.writeToFile(bitMatrix, format, file);// 输出图像
    	}else if(v.getText().toString().equals(getResources().getString(R.string.user_edit_cancel_btn))){
    		
    	}
    }*/
    
    private RequestCallBack callBackUser = new RequestCallBack() {

        @Override
        public void onFailure(HttpException arg0, String arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onSuccess(ResponseInfo arg0) {
            // TODO Auto-generated method stub
            User user = (User) Car91JsonUtil.getDTO(arg0.result.toString(), User.class);
            if (user != null) {
                if (user.getName() != null)
                    nickText.setText(user.getName());
                if (user.getRelname() != null)
                    nameText.setText(user.getRelname());
                if (user.getSex() == 1)
                    sexText.setText("男");
                else if (user.getSex() == 2)
                    sexText.setText("女");
                else
                    sexText.setText("保密");
                if (user.getBirthday() != null)
                    birthText.setText(user.getBirthday());
                if (user.getAddress() != null)
                    addressText.setText(user.getAddress());
                if (user.getZipcode() != null)
                    zipText.setText(user.getZipcode());
                if (user.getMobile() != null) {
                	 int between = user.getMobile().length()/2;
                	 try {
                		 user.setMobile(user.getMobile().substring(0, between-2) + 
                                 "****" + 
                                 user.getMobile().substring(between+2, user.getMobile().length()));
					} catch (Exception e) { }
                    if (user.getMobilecheck() == null)
                        mobileText.setText(user.getMobile() + " (未验证)");
                    else if (user.getMobilecheck())
                        mobileText.setText(user.getMobile());
                    else
                        mobileText.setText(user.getMobile() + " (未验证)");
                }
                if (user.getEmail() != null)
                    if (user.getEmailcheck() == null)
                        emailText.setText(user.getEmail() + " (未验证)");
                    else if (user.getEmailcheck())
                        emailText.setText(user.getEmail());
                    else
                        emailText.setText(user.getEmail() + " (未验证)");
            }
        }
    };
}
